package controllers

import (
	"databaseStudy/models"
	"databaseStudy/models/globalValues"
	beego "github.com/beego/beego/v2/server/web"
	"log"
)

type LoginController struct {
	beego.Controller
}

const (
	 SUCCESS = 200
	 FAIL    =400
)

type loginResponse struct {
	Status int    `json:"status"`
	Token string `json:"token"`
	Err   string `json:"err"`

}

func (c *LoginController) Post() {
	var userName = c.GetString("username")
	var password = c.GetString("password")
	log.Println(userName,password)
	var err = models.CheckLogin(userName, password)
	if err != nil {
		c.Data["json"]=&loginResponse{FAIL,"",err.Error()}
	}else{
		var token = globalValues.CreateToken()
		c.Data["json"]=&loginResponse{SUCCESS,token,""}
	}
	_=c.ServeJSON()

}
